package com.transaction.demo.message.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.transaction.demo.common.util.Result;
import com.transaction.demo.common.util.ResultPage;
import com.transaction.demo.message.service.TransactionMessageService;
import com.transaction.demo.message.vo.req.ReSendAllDeadMessageByQueueNameReqVo;
import com.transaction.demo.message.vo.req.TransactionMessageAddReqVo;
import com.transaction.demo.message.vo.req.TransactionMessageListReqVo;
import com.transaction.demo.message.vo.req.WaitOrSendMessageListReqVo;
import com.transaction.demo.message.vo.resp.TransactionMessageRespVo;

/**
 * 描述：消息相关操作
 * 作者：马庆智
 * 日期：2023/04/26 10:29:01
 */
@Api(tags = "消息记录")
@RestController
@RequestMapping("/transactionMessage")
public class TransactionMessageController {

	@Autowired
	private TransactionMessageService transactionMessageService;

	/**
	 * 查询列表
	 *
	 * @param record 请求参数
	 * @return TransactionMessageResp
	 * @author 马庆智
	 * @date: 2023/04/26 10:29:01
	 */
	@ApiOperation("查询列表")
	@PostMapping("/queryTransactionMessageList")
	public Result<ResultPage<TransactionMessageRespVo>> queryTransactionMessageList(
			@RequestBody @Valid TransactionMessageListReqVo record) {
		return Result.success(transactionMessageService.queryTransactionMessageList(record));
	}

	/**
	 * 查询待确认或者发送中的的消息
	 *
	 * @param record 请求参数
	 * @return TransactionMessageResp
	 * @author 马庆智
	 * @date: 2023/04/26 10:29:01
	 */
	@ApiOperation("查询待确认或者发送中的的消息")
	@PostMapping("/queryWaitOrSendMessageList")
	public Result<ResultPage<TransactionMessageRespVo>> queryWaitOrSendMessageList(
			@RequestBody @Valid WaitOrSendMessageListReqVo record) {
		return Result.success(transactionMessageService.queryWaitOrSendMessageList(record));
	}


	/**
	 * 预存储消息.
	 * @param transactionMessage 消息
	 * @return id
	 * @author 马庆智
	 * @date: 2023/4/27 9:55
	 */
	@ApiOperation("预存储消息")
	@PostMapping("/queryTransactionMessage")
	public Result<Integer> saveMessageWaitingConfirm(
			@RequestBody @Valid TransactionMessageAddReqVo transactionMessage) {
		return Result.success(transactionMessageService.saveMessageWaitingConfirm(transactionMessage));
	}

	/**
	 * 确认并发送消息.
	 *
	 * @param messageId 消息id
	 * @return id
	 * @author 马庆智
	 * @date: 2023/4/27 10:01
	 */
	@ApiOperation("确认并发送消息")
	@GetMapping("/confirmAndSendMessage")
	public Result<Integer> confirmAndSendMessage(String messageId) {
		return Result.success(transactionMessageService.confirmAndSendMessage(messageId));
	}

	/**
	 * 存储并发送消息.
	 * @param transactionMessage 消息
	 * @return id
	 * @author 马庆智
	 * @date: 2023/4/27 9:55
	 */
	@ApiOperation("存储并发送消息")
	@PostMapping("/saveAndSendMessage")
	public Result<Integer> saveAndSendMessage(@RequestBody @Valid TransactionMessageAddReqVo transactionMessage) {
		return Result.success(transactionMessageService.saveAndSendMessage(transactionMessage));
	}

	/**
	 * 直接发送消息.
	 * @param transactionMessage 消息
	 * @return id
	 * @author 马庆智
	 * @date: 2023/4/27 9:55
	 */
	@ApiOperation("直接发送消息")
	@PostMapping("/directSendMessage")
	public Result<Integer> directSendMessage(@RequestBody @Valid TransactionMessageAddReqVo transactionMessage) {
		return Result.success(transactionMessageService.directSendMessage(transactionMessage));
	}

	/**
	 * 重发消息.
	 * @param transactionMessage 消息
	 * @return id
	 * @author 马庆智
	 * @date: 2023/4/27 9:55
	 */
	@ApiOperation("重发消息")
	@PostMapping("/reSendMessage")
	public Result<Integer> reSendMessage(@RequestBody @Valid TransactionMessageAddReqVo transactionMessage) {
		return Result.success(transactionMessageService.reSendMessage(transactionMessage));
	}

	/**
	 * 根据messageId重发某条消息.
	 *
	 * @param messageId 消息id
	 * @return id
	 * @author 马庆智
	 * @date: 2023/4/27 10:01
	 */
	@ApiOperation("根据messageId重发某条消息")
	@GetMapping("/reSendMessageByMessageId")
	public Result<Integer> reSendMessageByMessageId(String messageId) {
		return Result.success(transactionMessageService.reSendMessageByMessageId(messageId));
	}

	/**
	 * 将消息标记为死亡消息.
	 *
	 * @param messageId 消息id
	 * @author 马庆智
	 * @return id
	 * @date: 2023/4/27 10:01
	 */
	@ApiOperation("将消息标记为死亡消息")
	@PutMapping("/setMessageToAreadlyDead")
	public Result<Integer> setMessageToAreadlyDead(String messageId) {
		return Result.success(transactionMessageService.setMessageToAreadlyDead(messageId));
	}

	/**
	 * 根据消息ID获取消息.
	 *
	 * @param messageId 消息id
	 * @return TransactionMessageRespVo
	 * @author 马庆智
	 * @date: 2023/4/27 10:01
	 */
	@ApiOperation("根据消息ID获取消息")
	@GetMapping("/getMessageByMessageId")
	public Result<TransactionMessageRespVo> getMessageByMessageId(String messageId) {
		return Result.success(transactionMessageService.getMessageByMessageId(messageId));
	}

	/**
	 * 根据消息ID删除消息.
	 *
	 * @param messageId 消息id
	 *
	 * @author 马庆智
	 * @date: 2023/4/27 10:01
	 */
	@ApiOperation("根据消息ID删除消息")
	@DeleteMapping("/deleteMessageByMessageId")
	public Result<Integer> deleteMessageByMessageId(String messageId) {
		return Result.success(transactionMessageService.deleteMessageByMessageId(messageId));
	}

	/**
	 * 重发某个消息队列中的全部已死亡的消息.
	 *
	 * @param reqVo 参数
	 *
	 * @author 马庆智
	 * @date: 2023/4/27 10:01
	 */
	@ApiOperation("重发某个消息队列中的全部已死亡的消息")
	@PostMapping("/reSendAllDeadMessageByQueueName")
	public Result<Long> reSendAllDeadMessageByQueueName(
			@RequestBody @Valid ReSendAllDeadMessageByQueueNameReqVo reqVo) {
		return Result.success(transactionMessageService.reSendAllDeadMessageByQueueName(reqVo.getConsumerQueue(),
				reqVo.getMessageSendCount()));
	}
}